package com.atguigu.ssyx.search.receiver;

import com.atguigu.ssyx.common.mq.constant.MqConst;
import com.atguigu.ssyx.search.service.SkuService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


/**
 * @Title SkuReceiver
 * @Description
 * @Author echidna
 * @Create 2023/9/22
 **/
@Component
public class SkuReceiver {

    @Autowired
    private SkuService skuService;

    /**
     * 监听 上架
     *
     * @param skuId
     * @param message
     * @param channel
     * @throws IOException
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_GOODS_UPPER, durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_GOODS_DIRECT),
            key = {MqConst.ROUTING_GOODS_UPPER}
    ))
    public void upperSku(Long skuId, Message message, Channel channel) throws IOException {
        if (skuId != null) {
            //上架
            skuService.upperSku(skuId);
        }
        //手动确认消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = MqConst.QUEUE_GOODS_LOWER, durable = "true"),
            exchange = @Exchange(value = MqConst.EXCHANGE_GOODS_DIRECT),
            key = {MqConst.QUEUE_GOODS_LOWER}
    ))
    public void lowerku(Long skuId, Message message, Channel channel) throws IOException {
        if (skuId != null) {
            //上架
            skuService.lowerSku(skuId);
        }
        //手动确认消息
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

}
